﻿#include <string>
#include <cmath>
#include <corecrt_math_defines.h>

#include "../Data/ModelData.h"
#include "../Data/UIData.h"
#include "../Data/DataCenter.h"

#include "Design.h"

using namespace std;

bool Design::TrapezoidDesign(ModelData& modelData, const UIData *uiData)
{
    if(!uiData) return false;
    const UITrapezoidData* uiTData = dynamic_cast<const UITrapezoidData*>(uiData);
    if(!uiTData) return false;

    // 获取参数
    const string& name = uiTData->m_Name;
    double width = uiTData->m_Width;
    double height = uiTData->m_Height;
    double theta = uiTData->m_Theta;

    // 判断输入参数的合理性
    if (name.empty() || width <= 0 || height <= 0 || theta <= 0 || theta >= 90) 
        return false;

    // 计算顶点坐标（原点为中心点）
    double halfWidth = width / 2.0;
    double offset = height * std::tan(theta * M_PI / 180.0);
    
    modelData.m_Name = name;
    modelData.m_Thickness = 0.0;
    modelData.m_Direct = {0.0, 0.0, 1.0};
    modelData.m_Vertices.push_back({-halfWidth + offset, height/2, 0.0});
    modelData.m_Vertices.push_back({-halfWidth, -height/2, 0});
    modelData.m_Vertices.push_back({halfWidth, -height/2, 0});
    modelData.m_Vertices.push_back({halfWidth - offset, height/2, 0});    

    return true;
}

bool Design::ExtrudeDesign(ModelData& modelData, const UIData *uiData)
{
    if(!uiData) return false;
    const UIExtrudeData* uiEData = dynamic_cast<const UIExtrudeData*>(uiData);
    if(!uiEData) return false;

    DataCenter& dataCenter = DataCenter::getInstance();
    ModelData* SourceData = dataCenter.GetModelData(uiEData->m_Source);
    if(!SourceData) return false;

    // 判断输入参数的合理性
    if(SourceData->m_Vertices.empty() || abs(uiEData->m_Dis - 0) < 1e-6)
        return false;

    modelData.m_Name = uiEData->m_Name;
    modelData.m_Vertices = SourceData->m_Vertices;
    modelData.m_Thickness = uiEData->m_Dis;
    modelData.m_Direct =  {0.0, 0.0, 1.0};

    return true;
}
